// 1.登陆授权 2.退出登陆 vuex
import { authorize, logout } from '@/api/auth';
import localstorage from '@/cookie/local.storage';
// init state
export const AUTHRIZE = 'authrize';
export const LOGOUT = 'logout';

const state = {
  auth: {},
  token: null,
};

// getters
const getters = {
  getAuth: state => state.auth,
  getToken: state => (state.token ? state.token : localstorage.fetch('auth').accessToken),
};

// actions
const actions = {
  authrize(context, params) {
    // http api request
    const param = {
      grantType: 'password',
      scope: 'read',
      clientId: 'e2b6abfc-3576-4fe7-9f69-1f647169a827',
      clientSecret: 'e24e45c9-a7bc-4248-be78-17b32bea1544',
      userName: params.userName,
      password: params.password,
    };
    authorize(param)
    .then(data => context.commit('auth', data)) // commit(types.FETCH_FEEDS, data)
    .catch(err => (console.log('err-->', err)));
  },
  logout(context) {
    // http api request
    logout('');
    context.commit(LOGOUT);
  },
};

// mutations
const mutations = {
  auth(state, { data }) {
    // eslint-disable-next-line
    state.auth = data;
    // eslint-disable-next-line
    state.token = data.accessToken;
    localstorage.save('auth', state.auth);
  },
  logout() {
    console.log(state);
  },
};


export default {
  state,
  getters,
  actions,
  mutations,
};
